<?php


namespace app\common\model;


use think\Model;

class UserRelationModel extends Model
{
    protected $name = 'user_relation';

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [

    ];




    /**
     * 绑定用户关系
     *
     * tip:
     * 前端校验,满足下面三个条件就调一次接口,邀请码用过后清除
     * 1.用户登录了
     * 2.是用户身份
     * 3.获取到了邀请码
     *
     * @ApiParams string $invite_code 用户邀请码
     */
    static public function bindUserRelation($params, $user_id): array
    {
        extract($params);

        try {
            $params['user_id'] = $user_id;
            $params_str = JsonEn($params);
            addLog("[绑定用户关系开始][Params][{$params_str}]",'bindUserRelation');

            $user = model('app\common\model\User')->get($user_id);
            // 用户身份才进行绑定
            if($user->group_id == 1) {
                $user_relation = model('app\common\model\UserRelationModel')->get(['user_id' => $user_id]);

                $group_id = 1;
                $p_user_id = 0;
                $user_id_1 = 0;
                $user_id_2 = 0;
                $user_id_3 = 0;
                $user_id_4 = 0;

                // 查询父级
                if(!empty($invite_code)) {
                    $p_user = model('app\common\model\User')->get(['invite_code' => $invite_code]);
                    if($p_user) {
                        $p_user_id = $p_user->id;
                        // 先绑定父级ID
                        $user->pid = $p_user->id;
                        $user->save();

                        // 查询父级用户绑定关系,要关联父级这条线的关系
                        $p_user_relation = model('app\common\model\UserRelationModel')->get(['user_id' => $p_user->id]);
                        if($p_user_relation) {
                            switch ($p_user->group_id) {
                                case 1:
                                    $user_id_1 = $p_user->id;
                                    $user_id_2 = $p_user_relation->user_id_2;
                                    $user_id_3 = $p_user_relation->user_id_3;
                                    $user_id_4 = $p_user_relation->user_id_4;
                                    break;
                                case 2:
                                    $user_id_2 = $p_user->id;
                                    $user_id_3 = $p_user_relation->user_id_3;
                                    $user_id_4 = $p_user_relation->user_id_4;
                                    break;
                                case 3:
                                    $user_id_3 = $p_user->id;
                                    $user_id_4 = $p_user_relation->user_id_4;
                                    break;
                                case 4:
                                    $user_id_4 = $p_user->id;
                                    break;
                            }
                        }
                    }
                }

                $arr = [
                    'user_id' => $user_id,
                    'group_id' => $group_id,
                    'p_user_id' => $p_user_id,
                    'user_id_1' => $user_id_1,
                    'user_id_2' => $user_id_2,
                    'user_id_3' => $user_id_3,
                    'user_id_4' => $user_id_4,
                ];

                $arr_str = JsonEn($params_str);
                addLog("[绑定用户关系数据][Data][{$arr_str}]",'bindUserRelation');

                if(!$user_relation) {
                    model('app\common\model\UserRelationModel')->create($arr);
                }else{
                    if($user_relation->p_user_id == 0) {
                        $user_relation->user_id = $user_id;
                        $user_relation->group_id = $group_id;
                        $user_relation->p_user_id = $p_user_id;
                        $user_relation->user_id_1 = $user_id_1;
                        $user_relation->user_id_2 = $user_id_2;
                        $user_relation->user_id_3 = $user_id_3;
                        $user_relation->user_id_4 = $user_id_4;
                        $user_relation->save();
                    }
                }

            }

            addLog("[绑定用户关系结束]",'bindUserRelation');

            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getLine()
            ];
        }
    }


}